﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.IO;
using UnityEngine.UI;
using System;
using GoogleMobileAds.Api;

public class DataManager : MonoBehaviour
{
    private int m_AllDataWasLoaded;
    public Image JinDuImage;
    public static DataManager Instance;
    public LanguageCfg HLLanguageCfg = new LanguageCfg();
    public HurtSpendCfg hurtspendcfg = new HurtSpendCfg();
    public SpeedSpendCfg speedspendcfg = new SpeedSpendCfg();
    public int Gold, Lv, HurtID, SpeedID, XiaoMieNum, PlayTimes, AddHurt, XiaoMieBossNum;
    public int FinishFirst, FinishSecond, FinishThird, FinishFourth;
    public int HaveThird, HaveFourth;
    public string Times;
    public int GuDing;
    

    void Awake()
    {
        if (Instance != null)
        {
            return;
        }
        else
        {
            Instance = this;
        }

        DontDestroyOnLoad(gameObject);
        StartCoroutine(LoadOnlyByteData());
        StartCoroutine(LoadHurtByteData());
        StartCoroutine(LoadSpeedByteData());
        InitInfo();
        
      


    }
    private void Update()
    {

        if (m_AllDataWasLoaded == 3)
        {
            JinDuImage.fillAmount += Time.deltaTime;
            if (JinDuImage.fillAmount >= 1)
            {
                SceneManager.LoadScene("PlayScene");
                m_AllDataWasLoaded = 0;
            }
        }
        
            if (Input.GetKeyDown(KeyCode.Escape)||Input.GetKeyDown(KeyCode.Home))
            {
                SaveTime();
                Debug.Log("times==" + Times);
                Application.Quit();
            }
        
    }
    private IEnumerator LoadOnlyByteData()
    {

        yield return StartCoroutine(HLLanguageCfg.LoadData());//加载语言表

        m_AllDataWasLoaded++;


    }
    private IEnumerator LoadHurtByteData()
    {

        yield return StartCoroutine(hurtspendcfg.LoadData());//加载伤害表

        m_AllDataWasLoaded++;


    }
    private IEnumerator LoadSpeedByteData()
    {

        yield return StartCoroutine(speedspendcfg.LoadData());//加载速度表

        m_AllDataWasLoaded++;


    }
    /// <summary>
    /// 保存数据
    /// </summary>
    public void Save()
    {
        PlayerPrefs.SetInt("Golds", Gold);
        PlayerPrefs.SetInt("LV", Lv);
        PlayerPrefs.SetInt("SpeedId", SpeedID);
        PlayerPrefs.SetInt("HurtId", HurtID);
        PlayerPrefs.SetInt("XiaoMieNumber", XiaoMieNum);
        PlayerPrefs.SetInt("PlayNum", PlayTimes);
        PlayerPrefs.SetInt("AddHurtNum", AddHurt);
        PlayerPrefs.SetInt("XiaoMieBossNumber", XiaoMieBossNum);
        PlayerPrefs.SetInt("FinishOne", FinishFirst);
        PlayerPrefs.SetInt("FinishTwo", FinishSecond);
        PlayerPrefs.SetInt("FinishThree", FinishThird);
        PlayerPrefs.SetInt("FinishFour", FinishFourth);
        PlayerPrefs.SetInt("HaveThree", HaveThird);
        PlayerPrefs.SetInt("HaveFour", HaveFourth);

    }
    /// <summary>
    /// 储存时间
    /// </summary>
    public void SaveTime()
    {
        Times = DateTime.Now.ToString();
        PlayerPrefs.SetString("Times", Times);
    }
    /// <summary>
    /// 读取数据
    /// </summary>
    public void InitInfo()
    {
        Gold = PlayerPrefs.GetInt("Golds", 100);
        Lv = PlayerPrefs.GetInt("LV", 1);
        SpeedID = PlayerPrefs.GetInt("SpeedId", 10001);
        Debug.Log("SpeedID==" + SpeedID);
        HurtID = PlayerPrefs.GetInt("HurtId", 10001);
        Debug.Log("HurtID==" + HurtID);
        XiaoMieNum = PlayerPrefs.GetInt("XiaoMieNumber", 0);
        PlayTimes = PlayerPrefs.GetInt("PlayNum", 0);
        AddHurt = PlayerPrefs.GetInt("AddHurtNum", 0);
        XiaoMieBossNum = PlayerPrefs.GetInt("XiaoMieBossNumber", 0);
        FinishFirst = PlayerPrefs.GetInt("FinishOne", 0);
        FinishSecond = PlayerPrefs.GetInt("FinishTwo", 0);
        FinishThird = PlayerPrefs.GetInt("FinishThree", 0);
        FinishFourth = PlayerPrefs.GetInt("FinishFour", 0);
        HaveThird = PlayerPrefs.GetInt("HaveThree", 0);
        HaveFourth = PlayerPrefs.GetInt("HaveFour", 0);
        Times = PlayerPrefs.GetString("Times", DateTime.Now.ToString());
    }
    /// <summary>
    /// /清除数据
    /// </summary>
    public void ClearData()
    {
        PlayerPrefs.SetInt("XiaoMieNumber", 0);
        PlayerPrefs.SetInt("PlayNum", 0);
        PlayerPrefs.SetInt("AddHurtNum", 0);
        PlayerPrefs.SetInt("XiaoMieBossNumber", 0);
        PlayerPrefs.SetInt("FinishOne", 0);
        PlayerPrefs.SetInt("FinishTwo", 0);
        PlayerPrefs.SetInt("FinishThree", 0);
        PlayerPrefs.SetInt("FinishFour", 0);
    }





}
